package com.jlg.beans.page;

import java.util.Arrays;

/**
 * raw net page content
 * 
 * @author huazhang
 * @since 2011-03-26
 */
public final class Content
{

    private String url;
    private String encoding;
    private byte[] content;
    private Metadata metadata;

    public Content(String url, byte[] content)
    {

        if (url == null)
            throw new IllegalArgumentException("null url");
        if (content == null)
            throw new IllegalArgumentException("null content");

        this.url = url;
        this.content = content;
    }

    /** The url fetched. */
    public String getUrl()
    {
        return url;
    }

    /** The binary content retrieved. */
    public byte[] getContent()
    {
        return content;
    }

    public String getEncoding()
    {
        return encoding;
    }

    public void setEncoding(String encoding)
    {
        this.encoding = encoding;
    }

    public Metadata getMetadata()
    {
        return metadata;
    }

    /** Other protocol-specific data. */
    public void setMetadata(Metadata metadata)
    {
        this.metadata = metadata;
    }

    public void setContent(byte[] content)
    {
        this.content = content;
    }

    public boolean equals(Object o)
    {
        if (!(o instanceof Content))
        {
            return false;
        }
        Content that = (Content) o;
        return this.url.equals(that.url)
                && Arrays.equals(this.getContent(), that.getContent());
    }

    public String toString()
    {
        StringBuffer buffer = new StringBuffer();
        buffer.append("url: " + url + "\n");
        buffer.append("Content:\n");
        buffer.append(new String(content)); // try default encoding
        return buffer.toString();

    }
}
